package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.mapper.GoodsMapper;
import com.hqyj.mapper.GoodsModelMapper;
import com.hqyj.mapper.SpecificationsMapper;
import com.hqyj.mapper.UserMapper;
import com.hqyj.pojo.Goods;
import com.hqyj.pojo.GoodsModel;
import com.hqyj.pojo.Specifications;
import com.hqyj.pojo.User;
import com.hqyj.service.GoodsService;
import com.hqyj.utils.DateUtil;
import com.hqyj.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName GoodsServiceImpl
 * @Description TODO
 * @Author ZhuYouWan
 * @Date 2022/8/31 14:02
 * @Version 1.0
 **/
@Service
public class GoodsServiceImpl implements GoodsService {
    private static final int CACHE_LIFE_TIME = 60*10;

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsModelMapper goodsModelMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SpecificationsMapper specificationsMapper;
    @Override
    public Map<String, Object> findGoodsAll(int pageNum,int pageSize,String condition) {
        Map<String,Object> map = new HashMap<>();
        String key = "goodsAll" + pageNum;

        if (!"".equals(condition)){
            PageHelper.startPage(pageNum,pageSize);
            List<Goods> goodsList = goodsMapper.findGoodsAll(condition);
            PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList);
            map.put("code",200);
            map.put("goods",goodsPageInfo);
        }
        else if (!redisUtil.hasKey(key)){
            PageHelper.startPage(pageNum,pageSize);
            List<Goods> goodsList = goodsMapper.findGoodsAll(condition);
            PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList);
            map.put("code",200);
            map.put("goods",goodsPageInfo);
            redisUtil.hmset(key,map,CACHE_LIFE_TIME);
        }else {
            map = redisUtil.hmget(key);
        }

        return map;
    }

    @Override
    public Map<String, Object> findInfo() {
        List<User> users = userMapper.findUserName("");
        List<GoodsModel> goodsModels = goodsModelMapper.findAllGoodsModel("");
        List<Specifications> specificationsList = specificationsMapper.findSpecificationsAll();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",200);
        map.put("uName",users);
        map.put("mType",goodsModels);
        map.put("sNorms",specificationsList);
        return map;
    }

    @Override
    public Map<String, Object> insertGoods(Goods goods) {
        //goods.setGDate(DateUtil.nowDate());
        Integer ret = goodsMapper.insertGoods(goods);
        int uId = userMapper.findUserIdByName(goods.getUAccount()).getUId();
        int mId = goodsModelMapper.findGoodsModelBymType(goods.getMType()).getMId();
        int sId = specificationsMapper.findSpecificationsBysNorms(goods.getSNorms()).getSId();
        int gId = goodsMapper.findGoodId(goods.getGNo());
        Integer res = goodsMapper.insertGoodsContact(gId, mId, sId, uId);
        Map<String,Object> map = new HashMap<>();
        if (ret>0 && res>0){
            delCache();
            map.put("code",200);
            map.put("message",goods.getGName()+"添加成功");
        }else {
            delCache();
            map.put("code",-1);
            map.put("message",goods.getGName()+"添加失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteGoodsById(int gId) {
        Integer gRet = goodsMapper.deleteGoodsById(gId);
        Integer gcRet = goodsMapper.deleteGoodsContactBygId(gId);
        Map<String,Object> map = new HashMap<>();
        if (gcRet>0 && gRet>0){
            delCache();
            map.put("code",200);
            map.put("msg","删除成功");
        }else {
            map.put("code",-1);
            map.put("msg","删除失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> updateGoodsById(Goods goods) {
        int uId = userMapper.findUserIdByName(goods.getUAccount()).getUId();
        int mId = goodsModelMapper.findGoodsModelBymType(goods.getMType()).getMId();
        int sId = specificationsMapper.findSpecificationsBysNorms(goods.getSNorms()).getSId();
        int gId = goods.getGId();
        Integer gRet = goodsMapper.updateGoodsById(goods);
        Integer gcRet = goodsMapper.updateGoodsContact(gId, mId, sId, uId);
        Map<String,Object> map = new HashMap<>();
        if (gRet>0 && gcRet>0){
            delCache();
            map.put("code",200);
            map.put("msg","商品"+goods.getGName()+"更新成功");
        }else {
            map.put("code",-1);
            map.put("msg","商品"+goods.getGName()+"更新失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> updateGoodsInfo(int gId) {
        List<User> users = userMapper.findUserName("");
        List<GoodsModel> goodsModel = goodsModelMapper.findAllGoodsModel("");
        List<Specifications> specificationsAll = specificationsMapper.findSpecificationsAll();
        Goods goods = goodsMapper.findGoodsById(gId);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("gId",goods.getGId());
        map.put("gName",goods.getGName());
        map.put("gNo",goods.getGNo());
        map.put("uName",users);
        map.put("gCount",goods.getGCount());
        map.put("gDate",goods.getGDate());
        map.put("mType",goodsModel);
        map.put("sNorms",specificationsAll);
        return map;
    }

    @Override
    public Map<String, Object> findGoodsGNno(String gNo) {
        Goods goodsGNno = goodsMapper.findGoodsGNno(gNo);
        Map<String,Object> map = new HashMap<>();
        if (goodsGNno!=null){
            map.put("code",200);
            map.put("msg","商品编号"+gNo+"已存在，请重新输入！");
        }else {
            map.put("code",-1);
            map.put("msg",gNo+"不存在");
        }
        return map;
    }

    public void delCache(){
        redisUtil.del(redisUtil.keysLike("goodsAll"));
    }
}
